package com.huige.controller;

import com.huige.anno.Log;
import com.huige.pojo.Dept;
import com.huige.pojo.Result;
import com.huige.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: DeptController
 * Package: com.huige.controller
 * Description:
 *
 * @Author 朱志辉
 * @Create 2024/5/3 14:55
 * @Version 1.0
 */
/*
 * 部门管理Controller
 * */
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    //private static Logger log = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;


    /**
     * 查询部门数据
     * */
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)//指定请求方式为GET
    @GetMapping
    public Result list() {
        log.info("查询全部部门数据");

        //调用service查询部门数据
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }


    /**
     * 删除部门
     * */
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) throws Exception {
        log.info("根据ID来删除部门:{}", id);
        //调用service删除部门
        deptService.delete (id);
        return Result.success();
    }

    /**
     * 添加部门
     * */
    @Log
    @PostMapping
    public Result insert(@RequestBody Dept dept) {
        log.info("新增部门:{}",dept);
        //调用service新增部门
        deptService.insert(dept);
        return Result.success();
    }
    /**
    * 根据ID查询
    * */
    @GetMapping("/{id}")
    public Result getByID(@PathVariable Integer id) {
        log.info("获取部门ID:{}",id);
        Dept dept = deptService.getByID(id);deptService.update(dept);


        return Result.success(dept);
    }

    /**
    * 编辑部门
    * */
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门:{}",dept);

        //调用service修改部门
        deptService.update(dept);
        return Result.success();
    }

}





